home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Network Support Library
/
RoseWare - Network Support Library.iso
/
apidev
/
locks.arc
/
PHYS_NET.C
< prev
next >
Wrap
Text File
|
1989-05-11
|
3KB
|
147 lines
/* Functions for physical record locking/unlocking for NETWORK files */
#include <dos.h>
#include <fcntl.h>
#include <stdio.h>
/* Return lo word of a long
*/
unsigned lo(l)
long l;
{
return(l&0xFFFF);
};
/* Return hi word of a long
*/
unsigned hi(l)
long l;
{
return(l>>16);
};
/* Set locking mode
*/
set_lock_mode(mode)
int mode;
{
struct REGPACK regs;
regs.r_ax = 0xC600 + mode;
intr(0x21,®s);
};
/* Log and lock a record
*/
int log_rec(handle,start,size,timeout,flgs)
int handle;
long start;
long size;
int timeout;
int flgs;
{
struct REGPACK regs;
regs.r_ax = 0xBC00|flgs;
regs.r_bx = handle;
regs.r_cx = hi(start);
regs.r_dx = lo(start);
regs.r_bp = timeout;
regs.r_si = hi(size);
regs.r_di = lo(size);
intr(0x21,®s);
return(lo(regs.r_ax));
};
/* Clear the records that have been logged or locked
*/
int clear_recs()
{
struct REGPACK regs;
regs.r_ax = 0xC400;
intr(0x21,®s);
return(lo(regs.r_ax));
};
main()
{
char c;
int start;
int size;
int timeout;
int handle;
char buff[256];
int i;
int flgs;
handle = open("test.dat",O_RDWR|O_DENYNONE|O_BINARY);
set_lock_mode(1);
while (!0)
{
clrscr();
printf("Enter (L)ock,(C)lear locks,(R)ead,(W)rite,(Q)uit :");
c=getch();
printf("\n");
if ((c==27)||(c=='q')||(c=='Q'))
{
set_lock_mode(0);
exit(0);
}
if ((c=='l')||(c=='L'))
{
printf("Enter start : ");
scanf("%d",&start);
printf("Enter size : ");
scanf("%d",&size);
printf("Enter timeout : ");
scanf("%d",&timeout);
printf("Bit 0 - log and lock, Bit 1 - non-exclusive\n");
printf("Enter flags : ");
scanf("%d",&flgs);
if (!log_rec(handle,(long)start,(long)size,timeout,flgs))
{
printf("LOGGING LOCKING ERROR");
getch();
}
}
if ((c=='c')||(c=='C'))
{
if(!clear_recs())
{
printf("CLEARING ERROR");
getch();
}
}
if ((c=='r')||(c=='R'))
{
printf("Enter start : ");
scanf("%d",&start);
printf("Enter size : ");
scanf("%d",&size);
lseek(handle,(long)start,SEEK_SET);
i = read(handle,&buff,size);
if (i!=size)
{
printf("READ ERROR\n");
getch();
}
for (i=0;i!=size;i++)
putch(buff[i]);
printf("\n");
getch();
}
if ((c=='w')||(c=='W'))
{
printf("Enter start : ");
scanf("%d",&start);
printf("Enter size : ");
scanf("%d",&size);
lseek(handle,(long)start,SEEK_SET);
printf("Enter in the %d characters\n",size);
for (i=0;i!=size;i++)
{
buff[i]=getch();
putch(buff[i]);
}
printf("\n");
write(handle,&buff,size);
}
}
};